package cn.bdqn.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.bdqn.pojo.Department;
import cn.bdqn.pojo.Post;
import cn.bdqn.pojo.PostType;
import cn.bdqn.service.DeptService;
import cn.bdqn.service.PostService;
import cn.bdqn.service.PostTypeService;
import cn.bdqn.tools.Page;

@Controller
@RequestMapping("/post")
public class PostController {
	
	    @Resource
	    private PostTypeService postTypeService;

	    @Resource
	    private PostService postService;

	    @Resource
	    private DeptService deptService;

	    private List<Department> deptList;
	    private List<Post> postList = new ArrayList<Post>();

	    //查询职位所有信息
	    @RequestMapping("findpost")
	    public String findPost(Model model,@RequestParam(required=false) String postName,
	                           @RequestParam(required=false,defaultValue="0") Integer deptId,@RequestParam(required=false,defaultValue="2") Integer isEnable,
	                           @RequestParam(value="pageIndex",required=false,defaultValue="1") Integer currentPageNo){

	        if("0".equals(Integer.toString(deptId))){
	            deptId=null;
	        }
	        if("2".equals(Integer.toString(isEnable))){
	            isEnable=null;
	        }
	        if("".equals(postName)){
	            postName=null;
	        }
	        Integer pageSize = 2;
	        int totalCount = 0;
	        totalCount = postService.count(postName, deptId, isEnable);
	        Page pages = new Page();
	        pages.setCurrPageNo(currentPageNo);
	        pages.setPageSize(pageSize);
	        pages.setTotalCount(totalCount);
	        int totalPageCount = pages.getTotalPageCount();
	        postList = postService.findPostAll(postName,deptId,isEnable,currentPageNo,pageSize);
	        deptList = deptService.findDept();
	        model.addAttribute("postList",postList);
	        model.addAttribute("page",pages);
	        model.addAttribute("deptList",deptList);
	        model.addAttribute("totalCount",totalCount);
	        return "job-placement";
	    }

	    //查询职位详情信息
	    @RequestMapping("getpost")
	    public String getPost(Model model,@RequestParam Integer id){
	        Post post = postService.getPost(id);
	        if(post!=null){
	            model.addAttribute("post",post);
	            return "job-placement-view";
	        }else{
	            return "redirect:/post/findpost";
	        }
	    }
	    //查询下拉框
	    @RequestMapping("findsel")
	    public String findSel(Model model){
	        deptList = deptService.findDept();
	        List<PostType> postTypes = postTypeService.findPostTypeAll();
	        if(deptList!=null && postTypes!=null){
	            model.addAttribute("deptList",deptList);
	            model.addAttribute("postTypes",postTypes);
	            return "job-placement-add";
	        }else{
	            return "redirect:/post/findpost";
	        }
	    }
	    //新增职位信息
	    @RequestMapping("getaddpost")
	    public String getAddPost(Post post){
	        if(post.getIsEnable()==null){
	            post.setIsEnable(1);
	        }
	        int flag = postService.getAdd(post);
	        if(flag == 1) {
	            return "redirect:/post/findpost";
	        }else {
	            return "redirect:/post/findsel";
	        }
	    }
	    //获取修改职位页面信息
	    @RequestMapping("modifypost")
	    public String modifyPost(Model model,@RequestParam Integer id){
	        Post post = postService.getPost(id);
	        deptList = deptService.findDept();
	        List<PostType> postTypes = postTypeService.findPostTypeAll();
	        if(post!=null&&deptList!=null && postTypes!=null){
	            model.addAttribute("post",post);
	            model.addAttribute("deptList",deptList);
	            model.addAttribute("postTypes",postTypes);
	            return "job-placement-modify";
	        }else{
	            return "redirect:/post/findpost";
	        }
	    }
	    //修改职位信息
	    @RequestMapping("modify")
	    public String modify(Post post){
	        if(post.getIsEnable()==null){
	            post.setIsEnable(1);
	        }
	        int flag = postService.modifyPost(post);
	        if(flag == 1) {
	            return "redirect:/post/findpost";
	        }else {
	            return "redirect:/post/modifypost";
	        }
	    }
	    //删除职位信息
	    @RequestMapping("delpost")
	    @ResponseBody
	    public String delPost(@RequestParam Integer postId){
	        int flag = postService.delPost(postId);
	        if(flag == 1) {
	            return "true";
	        }else {
	            return "false";
	        }
	    }
	
}
